package cn.edu.jsu.zjj.model.utils;

import cn.edu.jsu.zjj.model.entity.User;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;

public class OnceInformationUtils {

    public static void exportOnceInformation(User user) {

        String fileUrl = "http://example.com/template.docx"; // 替换为实际上传的URL
        try (FileInputStream fis = urlToFileInputStream(fileUrl)) {
            XWPFDocument document = new XWPFDocument(fis);
            for (XWPFParagraph paragraph : document.getParagraphs()) {
                for (XWPFRun run : paragraph.getRuns()) {
                    String text = run.getText(0);
                    if (text != null) {
                        text = text.replace("#{name}", user.getName()) // 替换占位符
                                .replace("#{sex}", Objects.equals(user.getSex(), "0") ? "男" : "女") // 替换占位符
                                .replace("#{nationality}", user.getNationality())
                                .replace("#{birthday}", user.getBirthday().toString())
                                .replace("#{address}", user.getAddress())
                                .replace("#{political_status}", user.getPoliticalStatus())
                                .replace("#{post]", user.getPost())
                                .replace("#{teaching_subjects}",user.getTeachingSubjects())
                                .replace("#{entry_time}",user.getEntryTime().toString())
                                .replace("#{avatar}",user.getAvatar())//这里需要处理图像，以二进制流写入
                                //这里空了两个：专业技术职称,何时评定
                                .replace("#{degree}",user.getDegree())
                                .replace("#{academic_degree}",user.getAcademicDegree())
                                .replace("#{alma_mater+major}",user.getAlmaMater()+user.getMajor())//这里有问题,差一个毕业时间
                                .replace("#{type}",user.getType())
                                //空一个社会兼职情况
                                .replace("#{mail_address}",user.getMailAddress())
                                .replace("#{phone_number}",user.getPhoneNumber())
                                .replace("#{wx_number}",user.getWxNumber())
                                .replace("#{qq}",user.getQq());


                        run.setText(text, 0);
                    }
                }
            }

            try (FileOutputStream fos = new FileOutputStream("filled_document.docx")) {
                document.write(fos); // 写入数据
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static FileInputStream urlToFileInputStream(String fileUrl) throws IOException {
        // 创建临时文件
        File tempFile = Files.createTempFile("temp", ".docx").toFile();
        tempFile.deleteOnExit(); // 确保临时文件在 JVM 退出时被删除

        // 下载文件到临时文件
        downloadFile(fileUrl, tempFile);

        // 创建 FileInputStream
        return new FileInputStream(tempFile);
    }

    private static void downloadFile(String fileUrl, File targetFile) throws IOException {
        URL url = new URL(fileUrl);
        try (ReadableByteChannel rbc = Channels.newChannel(url.openStream());
             FileOutputStream fos = new FileOutputStream(targetFile)) {
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        }
    }

}
